{
 "cells": [
  {
   "cell_type": "markdown",
   "source": [
    "# 手机定价(分类)\n",
    "<p>问题背景:随着互联网与信息技术的不断发展,手机已经与人们的生活,工作，学习等方面高度绑定。手机已成为刚需!.因此,也有许多商人加入到手机营销行业中,假设有一个老板，他的名字叫张三.张三新开了一家手机店。但是,手机如何定价对他而言是一个比较复杂的问题.他收集了一批手机的数据,手机的价格有四个价位(0-3表示)。我尝试用机器学习的方法帮他做手机的产品定价</p>\n",
    "\n",
    "## 准备数据"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(1500, 20)\n",
      "(500, 20)\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "from sklearn.model_selection import train_test_split\n",
    "filename = 'F:/机器学习数据集/phone_train.csv'\n",
    "with open(filename,encoding='utf-8') as f1:\n",
    "    phone_data = np.loadtxt(f1,skiprows=1,delimiter=',')\n",
    "\n",
    "X_train,X_test,y_train,y_test = train_test_split(phone_data[:,:-1],phone_data[:,-1],random_state=0,shuffle=True)\n",
    "print(X_train.shape)\n",
    "print(X_test.shape)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-10-03T03:12:08.360899300Z",
     "start_time": "2023-10-03T03:11:54.956407Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 尝试使用神经网络"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "测试集精度 0.932\n"
     ]
    }
   ],
   "source": [
    "from sklearn.neural_network import MLPClassifier\n",
    "from sklearn.preprocessing import StandardScaler,MinMaxScaler\n",
    "from sklearn.pipeline import Pipeline\n",
    "# pipe_mlp = Pipeline([('scaler',StandardScaler()),('mlp',MLPClassifier(max_iter=1000))])\n",
    "pipe_mlp = Pipeline([('scaler',MinMaxScaler()),('mlp',MLPClassifier(max_iter=1000))])\n",
    "pipe_mlp.fit(X_train,y_train)\n",
    "print(\"测试集精度\",round(pipe_mlp.score(X_test,y_test),3))"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-10-02T08:22:11.282219600Z",
     "start_time": "2023-10-02T08:22:01.053230600Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 神经网络的参数最优化"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "最佳参数组合: {'mlp__activation': 'logistic', 'mlp__hidden_layer_sizes': (100, 100), 'mlp__learning_rate_init': 0.001, 'mlp__solver': 'adam'}\n",
      "最佳验证集精度: 0.973\n",
      "测试集精度: 0.964\n"
     ]
    }
   ],
   "source": [
    "from sklearn.model_selection import GridSearchCV\n",
    "params_mlp = {'mlp__learning_rate_init':[0.0001,0.001,0.01,0.1],'mlp__solver':['lbfgs','adam'],\n",
    "              'mlp__activation':['relu','tanh','logistic'],\n",
    "              'mlp__hidden_layer_sizes':[(100,),(80,),(120,),(100,100)]\n",
    "              }\n",
    "grid_mlp = GridSearchCV(pipe_mlp,param_grid=params_mlp,n_jobs=-1)\n",
    "grid_mlp.fit(X_train,y_train)\n",
    "print(\"最佳参数组合:\",grid_mlp.best_params_)\n",
    "print(\"最佳验证集精度:\",round(grid_mlp.best_score_,3))\n",
    "print(\"测试集精度:\",round(grid_mlp.score(X_test,y_test),3))"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-10-02T08:37:05.682143Z",
     "start_time": "2023-10-02T08:30:35.257714800Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 使用随机森林"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "随机森林训练集精度: 1.0\n",
      "随机森林测试集精度: 0.878\n"
     ]
    }
   ],
   "source": [
    "from sklearn.preprocessing import MinMaxScaler\n",
    "from sklearn.pipeline import Pipeline\n",
    "from sklearn.ensemble import RandomForestClassifier\n",
    "\n",
    "pipe_rf = Pipeline([('scaler',MinMaxScaler()),('rf',RandomForestClassifier())])\n",
    "pipe_rf.fit(X_train,y_train)\n",
    "print('随机森林训练集精度:',round(pipe_rf.score(X_train,y_train),3))\n",
    "print(\"随机森林测试集精度:\",round(pipe_rf.score(X_test,y_test),3))"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-10-03T03:58:23.495958900Z",
     "start_time": "2023-10-03T03:58:21.835960800Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 随机森林调参(未使用PCA降维度)"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "随机森林最佳参数组合: {'rf__max_depth': 7, 'rf__max_features': 0.75, 'rf__n_estimators': 100}\n",
      "随机森林最佳验证集精度: 0.88\n",
      "随机森林测试集精度: 0.904\n"
     ]
    }
   ],
   "source": [
    "from sklearn.model_selection import GridSearchCV\n",
    "params_rf = {\n",
    "    'rf__n_estimators':[50,80,100,120,150],\n",
    "    'rf__max_depth':[4,5,6,7],\n",
    "    'rf__max_features':[0.5,0.75,1.0]\n",
    "}\n",
    "grid_rf = GridSearchCV(pipe_rf,param_grid=params_rf,n_jobs=-1)\n",
    "grid_rf.fit(X_train,y_train)\n",
    "print(\"随机森林最佳参数组合:\",grid_rf.best_params_)\n",
    "print('随机森林最佳验证集精度:',round(grid_rf.best_score_,3))\n",
    "print(\"随机森林测试集精度:\",round(grid_rf.score(X_test,y_test),3))"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-10-03T04:41:54.741164400Z",
     "start_time": "2023-10-03T04:41:28.966729300Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "# 随机森林调参(使用PCA)"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "随机森林最佳参数组合: {'rf__max_depth': 7, 'rf__max_features': 0.75, 'rf__n_estimators': 150}\n",
      "随机森林最佳验证集精度: 0.729\n",
      "随机森林测试集精度: 0.718\n"
     ]
    }
   ],
   "source": [
    "from sklearn.model_selection import GridSearchCV\n",
    "from sklearn.decomposition import PCA\n",
    "\n",
    "params_rf = {\n",
    "    'rf__n_estimators':[50,80,100,120,150],\n",
    "    'rf__max_depth':[4,5,6,7],\n",
    "    'rf__max_features':[0.5,0.75,1.0]\n",
    "}\n",
    "pipe_pca_rf = Pipeline([('scaler',MinMaxScaler()),('pca',PCA()),('rf',RandomForestClassifier())])\n",
    "grid_rf = GridSearchCV(pipe_pca_rf,param_grid=params_rf,n_jobs=-1)\n",
    "grid_rf.fit(X_train,y_train)\n",
    "print(\"随机森林最佳参数组合:\",grid_rf.best_params_)\n",
    "print('随机森林最佳验证集精度:',round(grid_rf.best_score_,3))\n",
    "print(\"随机森林测试集精度:\",round(grid_rf.score(X_test,y_test),3))"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-10-03T04:44:54.626325500Z",
     "start_time": "2023-10-03T04:44:12.526325200Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "# 使用AdaBoost算法"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "markdown",
   "source": [],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "ada训练集精度: 0.854\n",
      "ada测试集精度: 0.8\n"
     ]
    }
   ],
   "source": [
    "from sklearn.ensemble import AdaBoostClassifier\n",
    "from sklearn.tree import DecisionTreeClassifier\n",
    "ada = AdaBoostClassifier(base_estimator=(DecisionTreeClassifier(max_depth=3)))\n",
    "ada.fit(X_train,y_train)\n",
    "print('ada训练集精度:',round(ada.score(X_train,y_train),3))\n",
    "print(\"ada测试集精度:\",round(ada.score(X_test,y_test),3))"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-10-03T08:07:59.263826400Z",
     "start_time": "2023-10-03T08:07:58.837825500Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "## ADABoost算法，使用PCA降维"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "ada最佳参数组合: {'ada__base_estimator': DecisionTreeClassifier(max_depth=6), 'ada__n_estimators': 100}\n",
      "ada验证集最佳精度: 0.874\n",
      "ada测试集精度: 0.896\n"
     ]
    }
   ],
   "source": [
    "from sklearn.ensemble import AdaBoostClassifier\n",
    "from sklearn.tree import DecisionTreeClassifier\n",
    "from sklearn.pipeline import Pipeline\n",
    "from sklearn.decomposition import PCA\n",
    "from sklearn.preprocessing import MinMaxScaler\n",
    "pipe_ada =Pipeline([('pca',PCA(n_components=16)),('ada',AdaBoostClassifier())])\n",
    "params_ada = {'ada__base_estimator':[DecisionTreeClassifier(max_depth=3),DecisionTreeClassifier(max_depth=4),\n",
    "                                DecisionTreeClassifier(max_depth=5),DecisionTreeClassifier(max_depth=6)\n",
    "                                ],\n",
    "              'ada__n_estimators':[50,75,100]\n",
    "              }\n",
    "grid_ada = GridSearchCV(pipe_ada,param_grid=params_ada,n_jobs=-1)\n",
    "grid_ada.fit(X_train,y_train)\n",
    "print('ada最佳参数组合:',grid_ada.best_params_)\n",
    "print('ada验证集最佳精度:',round(grid_ada.best_score_,3))\n",
    "print(\"ada测试集精度:\",round(grid_ada.score(X_test,y_test),3))"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-10-03T12:49:00.230161800Z",
     "start_time": "2023-10-03T12:48:49.218149700Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "# 调整adaBoost参数"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "ada最佳参数组合: {'base_estimator': DecisionTreeClassifier(max_depth=6), 'n_estimators': 100}\n",
      "ada验证集精度: 0.888\n",
      "ada测试集精度: 0.906\n"
     ]
    }
   ],
   "source": [
    "params_ada = {'base_estimator':[DecisionTreeClassifier(max_depth=3),DecisionTreeClassifier(max_depth=4),\n",
    "                                DecisionTreeClassifier(max_depth=5),DecisionTreeClassifier(max_depth=6)\n",
    "                                ],\n",
    "              'n_estimators':[50,75,100]\n",
    "              }\n",
    "grid_ada = GridSearchCV(AdaBoostClassifier(),param_grid=params_ada,n_jobs=-1)\n",
    "grid_ada.fit(X_train,y_train)\n",
    "print(\"ada最佳参数组合:\",grid_ada.best_params_)\n",
    "print(\"ada验证集精度:\",round(grid_ada.best_score_,3))\n",
    "print('ada测试集精度:',round(grid_ada.score(X_test,y_test),3))"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-10-03T08:13:06.775347100Z",
     "start_time": "2023-10-03T08:12:59.710311Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "# 梯度提升树"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "grad验证集精度: 0.999\n",
      "grad测试集精度: 0.922\n"
     ]
    }
   ],
   "source": [
    "from sklearn.ensemble import GradientBoostingClassifier\n",
    "pipe_grad = Pipeline([('scaler',MinMaxScaler()),('grad',GradientBoostingClassifier())])\n",
    "pipe_grad.fit(X_train,y_train)\n",
    "print('grad验证集精度:',round(pipe_grad.score(X_train,y_train),3))\n",
    "print('grad测试集精度:',round(pipe_grad.score(X_test,y_test),3))"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-10-03T04:07:47.867742600Z",
     "start_time": "2023-10-03T04:07:44.946909500Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.999\n",
      "0.922\n"
     ]
    }
   ],
   "source": [
    "grad = GradientBoostingClassifier()\n",
    "grad.fit(X_train,y_train)\n",
    "print(round(grad.score(X_train,y_train),3))\n",
    "print(round(grad.score(X_test,y_test),3))"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-10-03T04:17:15.656840500Z",
     "start_time": "2023-10-03T04:17:13.723828600Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 梯度提升树调参"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "grad最佳参数组合: {'max_depth': 3, 'n_estimators': 150}\n",
      "grad验证集最佳精度: 0.89\n",
      "grad测试集精度: 0.922\n"
     ]
    }
   ],
   "source": [
    "params_grad = {\n",
    "    'max_depth':[3,4,5,6],\n",
    "    'n_estimators':[50,100,150]\n",
    "}\n",
    "grid_grad = GridSearchCV(GradientBoostingClassifier(),param_grid=params_grad,n_jobs=-1)\n",
    "grid_grad.fit(X_train,y_train)\n",
    "print(\"grad最佳参数组合:\",grid_grad.best_params_)\n",
    "print(\"grad验证集最佳精度:\",round(grid_grad.best_score_,3))\n",
    "print(\"grad测试集精度:\",round(grid_grad.score(X_test,y_test),3))"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-10-03T04:26:31.601860500Z",
     "start_time": "2023-10-03T04:25:59.672869300Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 尝试使用支持向量机"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "svc训练集精度: 0.969\n",
      "svc测试集精度: 0.852\n"
     ]
    }
   ],
   "source": [
    "from sklearn.svm import SVC\n",
    "pipe_svc = Pipeline([('scaler',MinMaxScaler()),('svc',SVC())])\n",
    "pipe_svc.fit(X_train,y_train)\n",
    "print(\"svc训练集精度:\",round(pipe_svc.score(X_train,y_train),3))\n",
    "print(\"svc测试集精度:\",round(pipe_svc.score(X_test,y_test),3))"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-10-03T04:34:47.470577400Z",
     "start_time": "2023-10-03T04:34:47.022572200Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "svc最佳参数组合: {'svc__C': 92, 'svc__gamma': 0.01}\n",
      "svc最佳验证集精度: 0.939\n",
      "svc测试集精度: 0.954\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "params_svc = {\n",
    "    'svc__C':np.arange(90,100,1),\n",
    "    'svc__gamma':[0.001,0.01,0.1,1]\n",
    "}\n",
    "pipe_svc = Pipeline([('scaler',MinMaxScaler()),('svc',SVC())])\n",
    "grid_svc = GridSearchCV(pipe_svc,param_grid=params_svc,n_jobs=-1)\n",
    "grid_svc.fit(X_train,y_train)\n",
    "print(\"svc最佳参数组合:\",grid_svc.best_params_)\n",
    "print(\"svc最佳验证集精度:\",round(grid_svc.best_score_,3))\n",
    "print('svc测试集精度:',round(grid_svc.score(X_test,y_test),3))"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-10-03T12:54:44.040353300Z",
     "start_time": "2023-10-03T12:54:40.669359400Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [],
   "metadata": {
    "collapsed": false
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
